package cn.fch.gotrip.service.impl;

import cn.fch.gotrip.dao.*;
import cn.fch.gotrip.entity.Hotel;
import cn.fch.gotrip.entity.HotelOrder;
import cn.fch.gotrip.entity.Room;
import cn.fch.gotrip.exception.NotFoundException;
import cn.fch.gotrip.exception.ValueException;
import cn.fch.gotrip.service.HotelOrderService;
import cn.fch.gotrip.util.OrderIdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.regex.Pattern;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-25 19:51
 **/
@Service
public class HotelOrderServiceImpl implements HotelOrderService {

    @Autowired
    private OrderIdUtil orderIdUtil;

    @Autowired
    private HotelOrderDao hotelOrderDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private HotelDao hotelDao;

    @Autowired
    private RoomDao roomDao;

    @Override
    public boolean creatHotelOrder(HotelOrder hotelOrder) {
        if (!userDao.isExistById(hotelOrder.getUser().getId())) {
            throw new ValueException("无此用户");
        }
        Hotel hotel = hotelDao.queryHotelById(hotelOrder.getHotel().getId());
        if (null == hotel){
            throw new ValueException("无此酒店");
        }
        Room room = roomDao.queryRoomById(hotelOrder.getRoom().getId());
        if (null == room){
            throw new ValueException("无此房间");
        }
        if (!room.getHotel().getId().equals(hotel.getId())){
            throw new ValueException("房间错误");
        }
        Date now = new Date();
        if (hotelOrder.getInDate().before(now)) {
            throw new ValueException("日期在今天之前");
        }
//        now = new Date(now.getTime() - 1000 * 60 * 60 * 24 * new Random().nextInt(100) + 1);
        hotelOrder.setType(new HotelOrder.Type(1));
        hotelOrder.setCreatTime(now);
        hotelOrder.setPrice(hotel.getPrice().add(room.getPrice().multiply(BigDecimal.valueOf(hotelOrder.getDays()))));
        hotelOrder.setId(orderIdUtil.creatHotelOrderIdLength21(now));
        return hotelOrderDao.creatHotelOrder(hotelOrder) > 0;
    }

    @Override
    public boolean deleteOrder(String id) {
        if (!isNumber(id)){
            throw new ValueException("不合法的id");
        }
        return hotelOrderDao.deleteOrder(id) > 0;
    }

    @Override
    public PageInfo<HotelOrder> queryByCreatAndType(Integer type, Integer pageNum, Integer pageSize, Long start, Long end) {
        if (start > end){
            throw new ValueException("时间错误");
        }
        Date startTime = new Date(start);
        Date endTime = new Date(end);
        PageHelper.startPage(pageNum, pageSize);
        List<HotelOrder> orderList = hotelOrderDao.queryByCreatAndType(type, startTime, endTime);
        return new PageInfo<>(orderList);
    }

    @Override
    public PageInfo<HotelOrder> searchOrderById(String id, Integer pageNum, Integer pageSize) {
        if (!isNumber(id)){
            throw new ValueException("不合法的id");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<HotelOrder> orderList = hotelOrderDao.searchOrderById(id);
        return new PageInfo<>(orderList);
    }

    @Override
    public HotelOrder queryOrderById(String id) {
        if (!isNumber(id)){
            throw new ValueException("不合法的id");
        }
        return Optional
                .ofNullable(hotelOrderDao.queryOrderById(id))
                .orElseThrow(() -> new NotFoundException("未找到订单"));
    }

    private boolean isNumber(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}
